﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Chowpoo.Sql
{
    /// <summary>
    /// 提供通用访问DAL的BLL层
    /// Model约束:class, IDetach, new()
    /// DAL约束:LinqAccessor, new()
    /// </summary>
    /// <typeparam name="T1"></typeparam>
    /// <typeparam name="T2"></typeparam>
    /// <typeparam name="T3"></typeparam>
    public class LinqBizAccessor<T1, T2, T3>
        where T1 : class, IDetach, new()
        where T2 : LinqAccessor<T1>, new()
        where T3 : new()
    {
        private static T3 _instance = default(T3);
        private static object _InstanceLock = new object();

        /// <summary>
        /// 获取自身实例
        /// </summary>
        public static T3 Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_InstanceLock)
                    {
                        if (_instance == null)
                        {
                            _instance = new T3();
                        }
                    }
                }
                return _instance;
            }
        }

        private static T2 _dalInstance = default(T2);
        private static object _DalInstance = new object();

        /// <summary>
        /// 获取对应的DAL层实例
        /// </summary>
        protected static T2 DalInstance
        {
            get
            {
                if (_dalInstance == null)
                {
                    lock (_DalInstance)
                    {
                        if (_dalInstance == null)
                        {
                            _dalInstance = new T2();
                        }
                    }
                }
                return _dalInstance;
            }
        }

        public IQueryable<T1> GetList()
        {
            var re = DalInstance.SelectQueryable(o => true);
            return re;
        }

        public List<T1> GetAll()
        {
            var re = DalInstance.Select(o => true);
            return re;
        }

        public bool Del(IEnumerable<T1> list)
        {
            return DalInstance.Delete(list) != null;
        }

        public bool Del(T1 info)
        {
            return DalInstance.Delete(info) != null;
        }

        public bool DelAll()
        {
            return DalInstance.Delete(o => true) != null;
        }

        public bool DelAll(Expression<Func<T1, bool>> expression)
        {
            return DalInstance.Delete(expression) != null;
        }

        public bool Edit(T1 info)
        {
            return DalInstance.Update(info) != null;
        }

        public bool Edit(IEnumerable<T1> info)
        {
            return DalInstance.Update(info) != null;
        }

        public bool Add(T1 info)
        {
            return DalInstance.Add(info) != null;
        }

        public bool Add(IEnumerable<T1> list)
        {
            return DalInstance.Add(list) != null;
        }
    }
}
